// cd /projects/hsieh_project/proj_201809/code_2_201910/
// qstata --dofile=out_city_jobcr_city_emp_data.do --statatype=mp --cpucount=5 &

clear all
capture noi program drop _all
cd /projects/

di "Started at $S_TIME $S_DATE"

global rev_date: display %tdYYNNDD date("$S_DATE", "DMY")
di "${rev_date}"

global dir_proj "/projects/hsieh_project/proj_201809/"

global dir_do "${dir_proj}/201910_main/"
global dir_data "${dir_proj}/data/"
global dir_out "${dir_proj}/output/201910_main/"
noi capture mkdir ${dir_out}
global dir_outf "${dir_out}/city_jobcr_city_emp/"
capture mkdir ${dir_outf}

global year1 = 1977
global year2 = 2013

global gl_perc = "10"
local l_perc = "10"

// Load functions
do "/projects/hsieh_project/code_0_general/f_rounding.do"
do "/projects/hsieh_project/code_0_general/f_lpolyci.do"

/*
================================================================================
Script that aggergates job creation data to the city level to prepare for plotting.
*/

use "${dir_data}/cityind_sum_all_sel", clear
keep city city_code emp_city_${year1}
duplicates drop
save ${dir_outf}/city_emp, replace

clear
save ${dir_outf}/city_jobcr, replace emptyok

capture program drop data_jobcr
program data_jobcr
	args i_city c_cr
	
	//local i_city = "msa"
	//local c_cr = 0
	use "${dir_data}/jobcr_`i_city'_${year1}_${year2}", clear
	rename `i_city' city
	gen city_code = "`i_city'"
	
	drop if syear1 == ${year1} & syear2 == ${year2}
	gen jobcr = .
	// Refer to hsieh_project/code_0_general/m_unit_status.sas for more details on status code
	if "`c_cr'" == "0" {
		// Count exit and shrinkage as destruction
		replace jobcr = 2 if inlist(cind_plant_stat, 5, 6) // Entry into or exit from industry
		replace jobcr = 1 if inlist(cind_plant_stat, 1, 3) // Entry and expansion
		replace jobcr = 0 if inlist(cind_plant_stat, 2, 4) // Exit and shrinkage
	}
	else if "`c_cr'" == "1" {
		// Only count exit as destruction
		replace jobcr = 2 if inlist(cind_plant_stat, 5, 6) // Entry into or exit from industry
		replace jobcr = 1 if inlist(cind_plant_stat, 1, 3, 4) // Entry, expansion, and shrinkage
		replace jobcr = 0 if inlist(cind_plant_stat, 2) // Exit
	}
	else if "`c_cr'" == "2" {
		// Only count shrinkage as destruction
		replace jobcr = 2 if inlist(cind_plant_stat, 5, 6) // Entry into or exit from industry
		replace jobcr = 1 if inlist(cind_plant_stat, 1, 2, 3) // Entry, expansion, and exit
		replace jobcr = 0 if inlist(cind_plant_stat, 4) // Shrinkage
	}
	
	// Aggregate by city type, city, job creation, beginning year, ending year, and year
	collapse (sum) emp_* est_* pay_*, by(city_code city jobcr year syear1 syear2)
	
	// Calulcate the total values of selected variables by city year
	// Make the selected variables negative in the second year (because we want job destruction to be positive)
	sort syear1 syear2 city year
	foreach i_var of var emp_* est_* pay_* {
		by syear1 syear2 city year: egen `i_var'_city = total(`i_var')
		gen `i_var'_city_syear1_0 = `i_var'_city if year==syear1
		gen `i_var'_city_syear2_0 = `i_var'_city if year==syear2
		by syear1 syear2 city: egen `i_var'_city_syear1 = mean(`i_var'_city_syear1_0)
		by syear1 syear2 city: egen `i_var'_city_syear2 = mean(`i_var'_city_syear2_0)
		//replace `i_var' = - `i_var' if year==syear1
		replace `i_var' = - `i_var' if year==syear2
	}
	
	// Use *_s2_city_* as city level variable (these should be the same for different s*)
	rename (emp_s2_city_syear1 est_s2_city_syear1 pay_s2_city_syear1) (emp_city_syear1 est_city_syear1 pay_city_syear1)
	rename (emp_s2_city_syear2 est_s2_city_syear2 pay_s2_city_syear2) (emp_city_syear2 est_city_syear2 pay_city_syear2)
	drop emp_s*_city_* est_s*_city_* pay_s*_city_*
	
	// Aggregate by city type, city, job creation, beginning year, ending year (from levels to changes)
	collapse (sum) emp_s* est_s* pay_s* (first) emp_city_syear1 est_city_syear1 pay_city_syear1 emp_city_syear2 est_city_syear2 pay_city_syear2, by(city_code city jobcr syear1 syear2)
	merge m:1 city_code city using ${dir_outf}/city_emp, assert(match using) keep(match) nogen // Merge with city employment
	count if syear1 == ${year1} & emp_city_syear1 != emp_city_${year1}
	gen cr = `c_cr'
	append using ${dir_outf}/city_jobcr
	save ${dir_outf}/city_jobcr, replace
end

data_jobcr "msa" 0 
data_jobcr "msa" 1
data_jobcr "msa" 2
data_jobcr "fips" 0
data_jobcr "fips" 1
data_jobcr "fips" 2
data_jobcr "czone" 0
data_jobcr "czone" 1
data_jobcr "czone" 2
data_jobcr "msa1983" 0
data_jobcr "msa1983" 1
data_jobcr "msa1983" 2

di "Ended at $S_DATE $S_TIME"
// End of do file
